package com.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.entity.ProdType;
import com.service.ProdTypeService;
import com.utils.PageObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author aiou
 * @since 2025-06-12
 */
@Controller
@RequestMapping("/prodType")
public class ProdTypeController {
    @Autowired
    private ProdTypeService prodTypeService;

    //品种类别：分页查询
    @RequestMapping("/list")
    public String list(PageObject pageObject, Model model){
        model.addAttribute("page", this.prodTypeService.list(pageObject));
        return "prodTypeList";
    }

    //品种类别：新增/修改
    @PostMapping("/save")
    @ResponseBody
    public String save(ProdType prodType){
        ProdType one = this.prodTypeService.getOne(prodType.getProdTypeCode());
        if(prodType.getProdTypeId() == null){
            if(one != null) return "fail";
            boolean save = this.prodTypeService.save(prodType);
            if(save) return "success";
        } else {
            if(one != null && !one.getProdTypeId().equals(prodType.getProdTypeId())) return "fail";
            boolean updateById = this.prodTypeService.updateById(prodType);
            if(updateById) return "success";
        }
        return null;
    }

    //品种类别：删除
    @PostMapping("/delete")
    @ResponseBody
    public String delete(Integer id){
        boolean remove = this.prodTypeService.removeById(id);
        if(remove) return "success";
        return "fail";
    }
}

